package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICopyrightService;
import com.mdd.admin.validate.CopyrightCreateValidate;
import com.mdd.admin.validate.CopyrightUpdateValidate;
import com.mdd.admin.validate.CopyrightSearchValidate;
import com.mdd.admin.vo.CopyrightListedVo;
import com.mdd.admin.vo.CopyrightDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Copyright;
import com.mdd.common.mapper.CopyrightMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 提案管理版权实现类
 * @author LikeAdmin
 */
@Service
public class CopyrightServiceImpl implements ICopyrightService {
        
    @Resource
    CopyrightMapper copyrightMapper;

    /**
     * 提案管理版权列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CopyrightListedVo>
     */
    @Override
    public PageResult<CopyrightListedVo> list(PageValidate pageValidate, CopyrightSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Copyright> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        copyrightMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:proposition:str",
            "like:proposalName@proposal_name:str",
            "like:clientName@client_name:str",
            "=:reputationGrade@reputation_grade:str",
            "=:businessPersonnel@business_personnel:str",
            "=:businessAssistant@business_assistant:str",
            "=:internalSecurityPeople@internal_security_people:str",
            "=:caseType@case_type:str",
            "=:searchman:str",
            "between:searchTime@search_time:str",
            "=:externalCaseSource@external_case_source:str",
            "=:secondCaseSource@second_case_source:str",
            "=:caseTypeid@case_typeid:long",
            "=:businessPersonnelid@business_personnelid:long",
            "=:customerContactid@customer_contactid:long",
            "=:proposerid:long",
            "=:inventorInformationid@inventor_informationid:long",
            "=:isTechnicalDisclosure@is_technical_disclosure:str",
            "=:isBopis@is_BOPIS:str",
            "=:userId@user_id:long",
            "like:userName@user_name:str",
        });

        IPage<Copyright> iPage = copyrightMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CopyrightListedVo> list = new LinkedList<>();
        for(Copyright item : iPage.getRecords()) {
            CopyrightListedVo vo = new CopyrightListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 提案管理版权详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Copyright
     */
    @Override
    public CopyrightDetailVo detail(Integer id) {
        Copyright model = copyrightMapper.selectOne(
                new QueryWrapper<Copyright>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CopyrightDetailVo vo = new CopyrightDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 提案管理版权新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(CopyrightCreateValidate createValidate) {
        Copyright model = new Copyright();
        model.setProposition(createValidate.getProposition());
        model.setProposalName(createValidate.getProposalName());
        model.setClientName(createValidate.getClientName());
        model.setReputationGrade(createValidate.getReputationGrade());
        model.setBusinessPersonnel(createValidate.getBusinessPersonnel());
        model.setBusinessAssistant(createValidate.getBusinessAssistant());
        model.setInternalSecurityPeople(createValidate.getInternalSecurityPeople());
        model.setCaseType(createValidate.getCaseType());
        model.setSearchman(createValidate.getSearchman());
        model.setSearchTime(createValidate.getSearchTime());
        model.setExternalCaseSource(createValidate.getExternalCaseSource());
        model.setSecondCaseSource(createValidate.getSecondCaseSource());
        model.setCaseTypeid(createValidate.getCaseTypeid());
        model.setBusinessPersonnelid(createValidate.getBusinessPersonnelid());
        model.setCustomerContactid(createValidate.getCustomerContactid());
        model.setProposerid(createValidate.getProposerid());
        model.setInventorInformationid(createValidate.getInventorInformationid());
        model.setIsTechnicalDisclosure(createValidate.getIsTechnicalDisclosure());
        model.setIsBopis(createValidate.getIsBopis());
        model.setUserId(createValidate.getUserId());
        model.setUserName(createValidate.getUserName());
        copyrightMapper.insert(model);
    }

    /**
     * 提案管理版权编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(CopyrightUpdateValidate updateValidate) {
        Copyright model = copyrightMapper.selectOne(
                new QueryWrapper<Copyright>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setProposition(updateValidate.getProposition());
        model.setProposalName(updateValidate.getProposalName());
        model.setClientName(updateValidate.getClientName());
        model.setReputationGrade(updateValidate.getReputationGrade());
        model.setBusinessPersonnel(updateValidate.getBusinessPersonnel());
        model.setBusinessAssistant(updateValidate.getBusinessAssistant());
        model.setInternalSecurityPeople(updateValidate.getInternalSecurityPeople());
        model.setCaseType(updateValidate.getCaseType());
        model.setSearchman(updateValidate.getSearchman());
        model.setSearchTime(updateValidate.getSearchTime());
        model.setExternalCaseSource(updateValidate.getExternalCaseSource());
        model.setSecondCaseSource(updateValidate.getSecondCaseSource());
        model.setCaseTypeid(updateValidate.getCaseTypeid());
        model.setBusinessPersonnelid(updateValidate.getBusinessPersonnelid());
        model.setCustomerContactid(updateValidate.getCustomerContactid());
        model.setProposerid(updateValidate.getProposerid());
        model.setInventorInformationid(updateValidate.getInventorInformationid());
        model.setIsTechnicalDisclosure(updateValidate.getIsTechnicalDisclosure());
        model.setIsBopis(updateValidate.getIsBopis());
        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setId(updateValidate.getId());
        copyrightMapper.updateById(model);
    }

    /**
     * 提案管理版权删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Copyright model = copyrightMapper.selectOne(
                new QueryWrapper<Copyright>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        copyrightMapper.updateById(model);
    }

}
